﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.IO;
using System.Diagnostics;

namespace RssReader
{
    /// <summary>
    /// Interaction logic for Window3.xaml
    /// </summary>
    public partial class Window3 : Window
    {
        RelayCommand scrollCommand;


        public Window3()
        {
            InitializeComponent();

            scrollCommand = new RelayCommand(noparam => this.OnScroll());
            KeyGesture OpenKeyGesture = new KeyGesture(Key.S, ModifierKeys.Control);
            KeyBinding OpenCmdKeybinding = new KeyBinding(scrollCommand, OpenKeyGesture);
            this.InputBindings.Add(OpenCmdKeybinding);

        }


        public void OnScroll()
        {
            this.Posts.DoWork();
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove(); 
        }

        private void Posts_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListBox li = sender as ListBox;
            if (li != null)
            {
                Post p = li.SelectedItem as Post;
                if (p != null)
                {
                    MessageBox.Show("Shell " + p.URL); 
                } 
            } 
        }




    }

    
    public class HTMLStringToDocumentConverter : IValueConverter
    {


        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                string s = (string)value;

                string XAML = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(s, true);

                MemoryStream memory = new MemoryStream(XAML.Length);
                StreamWriter writer = new StreamWriter(memory);
                writer.Write(XAML);
                writer.Flush();
                memory.Seek(0, SeekOrigin.Begin);
                FlowDocument doc = System.Windows.Markup.XamlReader.Load(memory) as FlowDocument;                
                //doc.Blocks.Add(obj);
                return doc;
            }
            catch { }
            return null; 
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    } 
    public class ScrollabeListBox : ListBox 
    {

        private ScrollViewer _viewer; 
        public override void OnApplyTemplate()
        {
             base.OnApplyTemplate();
             _viewer = this.GetTemplateChild("scrollViewer") as ScrollViewer; 
        }

        public ScrollViewer ScrollViewr
        {
            get
            {
                return _viewer; 
            } 

        }

        public void DoWork()
        {


            Storyboard sb = _viewer.TryFindResource("AnimateScrollViewer") as Storyboard;
            if (sb != null)
            {
                DoubleAnimationUsingKeyFrames da = sb.Children[0] as DoubleAnimationUsingKeyFrames;
                if (da != null)
                {
                    da.KeyFrames[1].Value = _viewer.ExtentHeight; 
                    sb.Begin();
                } 
            } 
        } 

    }


    public class RelayCommand : ICommand
    {
        #region Fields

        readonly Action<object> _execute;
        readonly Predicate<object> _canExecute;

        #endregion // Fields

        #region Constructors

        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }
        #endregion // Constructors

        #region ICommand Members

        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        #endregion // ICommand Members
    }


  

    public static class DesignTimeData  
    {
        static Feed f = new Feed("http://blogs.msdn.com/tims/rss.xml"); 
        public static Feed SingleFeed
        {
            get
            {
                return f; 
            } 
        } 
    } 
}
